package com.petsystem.servlet;

import com.petsystem.service.UserService;
import com.petsystem.service.impl.UserServiceImpl;
import com.petsystem.vo.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AuthServlet extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 过滤编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");
        if ("login".equalsIgnoreCase(action)) { // 登录
            String username = request.getParameter("username");
            String password = request.getParameter("password");

            UserService userService = new UserServiceImpl();
            Map<String, Object> params = new HashMap<>();
            List<User> list = userService.list(params); // 直接获取列表
            for (User user : list) {
                if (user.getUsername().equals(username) && user.getPassword().equals(password)) { // 找到这个用户了
                    request.getSession().setAttribute("loginUser", user);
                    request.getSession().setMaxInactiveInterval(Integer.MAX_VALUE);
                    request.getRequestDispatcher("UserServlet").forward(request, response);
                    return;
                }
            }
            request.getSession().setAttribute("alert_msg", "错误：用户名或密码错误！");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else if ("register".equalsIgnoreCase(action)) { // 注册
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            UserService userService = new UserServiceImpl();
            Map<String, Object> params = new HashMap<>();
            List<User> list = userService.list(params); // 直接获取列表
            for (User user : list) {
                if (user.getUsername().equals(username)) { // 说明该用户名已存在，必须换个用户名才能注册
                    request.getSession().setAttribute("alert_msg", "错误：用户名已存在！");
                    request.getRequestDispatcher("register.jsp").forward(request, response);
                    return;
                }
            }
            User vo = new User();
            vo.setUsername(username);
            vo.setPassword(password);
            vo.setCreateBy(0L);
            vo.setUserType("普通用户");
            userService.add(vo);
            request.getSession().setAttribute("alert_msg", "注册成功！用户名：[" + username + "]");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        } else if ("logout".equalsIgnoreCase(action)) { // 登出
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("loginUser");
            if (user != null) {
                session.removeAttribute("loginUser");
            }
            response.sendRedirect("login.jsp");
        } else if ("resetPassword".equalsIgnoreCase(action)) { // 重置密码
            String msg;
            User loginUser = (User) request.getSession().getAttribute("loginUser");
            String oldPassword = request.getParameter("oldPassword");
            if (!loginUser.getPassword().equals(oldPassword)) {
                msg = "原密码错误！";
            } else {
                String newPassword = request.getParameter("newPassword");
                loginUser.setPassword(newPassword);
                UserService userService = new UserServiceImpl();
                userService.update(loginUser);
                msg = "修改成功！";
            }
            request.getSession().setAttribute("alert_msg", msg);
            request.getRequestDispatcher("reset_password.jsp").forward(request, response);
        } else {
            response.sendRedirect("login.jsp");
        }
    }

    /**
     * 处理Get请求
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}